package com.atguigu.ware.wares.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 配置库存需要的队列以及装换器以及二者的绑定关系
 */
@Configuration
public class MyRabbitCovConfig {

    //创建交换机
    @Bean
    public Exchange stockEventExchange() {
        //(String name, boolean durable, boolean autoDelete, Map<String, Object> arguments)
        return new TopicExchange("stock-event-exchange", true, false);
    }


    //创建延时队列
    @Bean
    public Queue stockDelayQueue() {
        //(String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
        Map<String, Object> arguments= new HashMap<>();
        arguments.put("x-message-ttl",120000);
        arguments.put("x-dead-letter-exchange","stock-event-exchange");
        arguments.put("x-dead-letter-routing-key","stock.release");
        return new Queue("stock.delay.queue",true,false,false,arguments);
    }

    //创建正常队列
    @Bean
    public Queue stockReleaseStockQueue() {
        return new Queue("stock.release.stock.queue",true,false,false);

    }

    //创建绑定关系
    //(String destination, Binding.DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments)
    @Bean
    public Binding stockLockedBinding(){
       return new Binding("stock.delay.queue",Binding.DestinationType.QUEUE
       ,"stock-event-exchange","stock.locked",null);
    }

    //创建绑定关系
    @Bean
    public Binding stockReleaseBinding(){
        return new Binding("stock.release.stock.queue",Binding.DestinationType.QUEUE
                ,"stock-event-exchange","stock.release.#",null);
    }
}
